<HTML><HEAD>
<!--
    --------------------------
    Strings to Arrays and Back
    --------------------------
-->

<SCRIPT LANGUAGE="JavaScript1.2"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)2000 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/

// String to Array
function stringArray(aString)
{
	return aString.split(':');
}
    
// Array to String
function arrayString(anArray, len)
{
	return anArray.join(':');
}

// Initialize Global Variables
myArray = new Array(50) // 50 should be big enough
arrayTop = 0              // Top of myArray

// Show the Array
function showArray()
{
    document.forms[0].myArray.value = arrayString(myArray, arrayTop)
    
    // prepare for next entry
    document.forms[0].input.focus()
    document.forms[0].input.select()
}

// Add value to array
function add()
{
    myArray[arrayTop]=""+document.forms[0].input.value
    arrayTop++ // increase top
    showArray()
}

// Clear the array
function clear()
{
    arrayTop = 0
    document.forms[0].input.value=''
    showArray()
}

// Enter a string
function enter()
{    
    var s = ""+document.forms[0].myArray.value
    var ra = new stringArray(s)
    arrayTop = ra.length
    for (var i = 0; i < arrayTop; i++) myArray[i] = ra[i]
    showArray()
    return true
}
    
// Sort the array using a simple bubblesort
function sort()
{
	myArray.sort();
    showArray()
}
<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
    onLoad="document.forms[0].input.focus();document.forms[0].input.select()">

<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
    ALIGN = CENTER>Strings to Arrays and Back II</H1></FONT>

<FONT COLOR="770000">
    This applet recapitulates <A HREF="STRARR.HTM">Strings to Arrays and Back</A>,
	using JavaScript 1.2 functions to reduce the amount of code you have to write.
    Just like in the other recipe: <ul>
    <li>Type 'f' and tap [Add to Array];
    <li>Type 'a' and tap [Add to Array];
    <li>Tap on the top text entry field to change focus;
    <li>Add in ':c:d:g:e:b';
    <li>Tap [Sort Array].
    </ul>
</FONT></BLOCKQUOTE>

<BR><BR>

</SCRIPT><CENTER><FORM><TABLE BORDER=1>

<TR>
<TD align=center  colspan=3><input type="text" name="myArray" value="" 
    size=40 onBlur="enter()">String</TD>
</TR>

<TR>
<TD align=center  colspan=3><input type="text" name="input" 
value="" size=8></TD>
</TR>

<TR>
<TD align=center>
<input type="button" value="Sort Array" onClick="sort()"></TD>
<TD align=center>
<input type="button" value="Add to Array" onClick="add()"></TD>
<TD align=center>
<input type="button" value="Clear Array" onClick="clear()"></TD>
</TR> 

</TABLE></FORM></CENTER>

<br><br>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
What follows shows how the code size changes when you use the native functions.  The left
side is the "old" code; the right side is the "new" code using native functionality.<p>
</FONT>

<CENTER><TABLE BORDER=1>
<TR>
<TD VALIGN="TOP">
<FONT COLOR="770000"><PRE>
// String to Array
function stringArray(aString)
{
    var i = 0
    var idx = 0
    var s = ""+aString
    
    if (s == "")
    {
        this.length = 0
        return this
    }
    
    // search for the colon to create each array item
    while ((idx = s.indexOf(':')) != -1)
    {
        this[i] = s.substring(0, idx)
        s = s.substring(idx+1,s.length)
        i++
    }
    
    // create final array item
    this[i]=s
    this.length = i+1
    
    return this
}
</PRE></FONT></TD>
<TD VALIGN="TOP"><FONT COLOR="770000"><PRE>
// String to Array
function stringArray(aString)
{
    return aString.split(':');
}
</PRE></FONT></TD>
</TR><TR>
<TD VALIGN="TOP"><FONT COLOR="770000"><PRE>
// Array to String
function arrayString(anArray, len)
{
    var s = ""
    
    for (var i = 0; i < (len - 1); i++)
        s += anArray[i]+":"
    if (len > 0)
        s += anArray[len - 1]
    return s
}
</PRE></FONT>
</TD>
<TD VALIGN="TOP"><FONT COLOR="770000"><PRE>
// Array to String
function arrayString(anArray, len)
{
    return anArray.join(':');
}
</PRE></FONT></TD>
</TR></TABLE></CENTER>
<h5>Copyright ©2000 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>